import exp from 'constants'
import { fetchWrapper } from './common'
export type QuerySaleIndicatorParams = {
  /**统计类型(1.月累计 2.年累计) 默认查月累计 */
  statistical: string
  /**产品类型，传产品名字，传空则查询全部 */
  productType: string
  year: string
  month: string
}

export type SaleMonthData = {
  map: {
    [idx: string]: {
      name: string
      score: string
      val: string
      percent: string
    }
  }
  name: string
}
export type CustomerItem = {
  name: string
  item1: string
  item2: string
  item3: string
  item4: string
  item5: string
  mtd: string
}

export type SaleIndicatorItem = {
	name: string;
	percent: string;
	target?: string;
	score: string;
	schedule: string;
	mtdInt: string;
	mtd: string;
	targetVal: number;
	scheduleVal: number;
	targetCompleteVal: number
};

export type GroupsItem = {
  count: number
  groupName: string
  root: SaleIndicatorItem
  sales: SaleIndicatorItem[]
}

export type SaleIndicatorRes = {
  groups: GroupsItem[]
  updateTime: string
}

export const getSaleIndicatorApi = (p: {
  url: string
  args: QuerySaleIndicatorParams
}) => {
  if (!p) return
  const { url, args } = p

  const queryObj = new URLSearchParams(args)
  const query = `?${queryObj.toString()}`
  return fetchWrapper<SaleIndicatorRes>(url + query)
}

export const getSaleCount = (url: string) => {
  return fetchWrapper<Record<string, string>>(url)
}

export type QuerySaleIndicatorDetailsParams = {
  year: string
  month: string
  /**销售人员姓名 */
  customerName: string
  /** tab name */
  groupName: string
  statistical: string
}

export type SaleIndicatorDetailsItem = {
	/**优势航线 */
	advantageVal: string;
	/**指标名称 */
	name: string;
	/**指标得分 */
	score: string;
	/**指标值 */
	value: string;
};

export type SeriesItem = {
  name: string
  data: string[]
}
export type SaleIndicatorDetailsRes = {
  /**数据列表 */
  itemList: SaleIndicatorDetailsItem[]
  /** 纵坐标明细数据 */
  series: SeriesItem[]
  x: string[]
  total: string
  totalScore: string
}

export const getSaleIndicatorDetail = (p: {
  url: string
  args: QuerySaleIndicatorDetailsParams
}) => {
  if (!p) return
  const { url, args } = p

  const queryObj = new URLSearchParams(args)
  const query = `?${queryObj.toString()}`
  return fetchWrapper<SaleIndicatorDetailsRes>(url + query)
}
